package com.jwthhealth_pub.old.report.view.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.common.preference.PreferenceKey;
import com.jwthhealth_pub.old.common.ImageLoader;
import com.jwthhealth_pub.old.report.model.resbody.SportSuggListResp;
import com.jwthhealth_pub.old.report.view.SportDetailExplActivity;
import com.jwthhealth_pub.old.report.view.SportSuggestActivity;

import java.util.List;

/**
 * 运动内容
 * Created by liuzhen on 2017/10/24.
 */

public class SportStepContentAdapter extends RecyclerView.Adapter {

    private final Activity mActivity;
    private List<SportSuggListResp.DataBeanX.DayListBean.DataBean.SonBean> mData;
    private int mCurrentStage;
    private int mSelStage;

    SportStepContentAdapter(SportSuggestActivity mActivity, List<SportSuggListResp.DataBeanX.DayListBean.DataBean> contentList, int adapterPosition, int mSelStage, int mSelDay) {
        this.mActivity = mActivity;
        this.mCurrentStage = adapterPosition;
        this.mSelStage = mSelStage;
        if (mSelDay != -1) {
            this.mData = contentList.get(mSelDay).getSon();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(mActivity, R.layout.item_sport_suggest_content, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;

        if (mSelStage != mCurrentStage) return;

        final SportSuggListResp.DataBeanX.DayListBean.DataBean.SonBean dataBean = mData.get(vh.getAdapterPosition());

        vh.clickLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataBean == null) return;
                String id = dataBean.getId();

                if (id != null && !dataBean.getVideo_id().equals("0")) {
                    Intent intent = new Intent(mActivity, SportDetailExplActivity.class);
                    intent.putExtra("id", id);
                    mActivity.startActivity(intent);
                }
            }
        });

        if (getItemCount() == 1 && dataBean.getName() != null) {
            vh.iconLayout.setVisibility(View.GONE);
            vh.rest.setText(dataBean.getName());
            vh.rest.setVisibility(View.VISIBLE);

            ViewGroup.LayoutParams layoutParams = vh.rest.getLayoutParams();
            layoutParams.width = App.preferenceUtil.getInt((PreferenceKey.SCREEN_WIDTH),  layoutParams.width);
            vh.rest.setLayoutParams(layoutParams);

            return;
        } else {
            vh.iconLayout.setVisibility(View.VISIBLE);
            vh.rest.setVisibility(View.GONE);
        }


        if (dataBean == null) return;

        String video_id = dataBean.getVideo_id();
        if (video_id != null) {
            String thumb = dataBean.getThumb();
            if (thumb != null && thumb.length() > 0)
                ImageLoader.picasso(thumb, vh.icon);
        }

        String name = dataBean.getName();
        if (name != null)
            vh.title.setText(dataBean.getName());

        String time_ci_shu = dataBean.getTime_ci_shu();
        if (time_ci_shu != null)
            vh.desc.setText(time_ci_shu);

        String rest_time = dataBean.getRest_time();
        if (rest_time != null)
            vh.time.setText(rest_time);
    }

    @Override
    public int getItemCount() {
        if (mData != null && mData.size() > 0)
            return mData.size();
        else
            return 0;
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout clickLayout, iconLayout, contentLayout;
        public ImageView icon;
        TextView title, desc, time, rest;

        public ViewHolder(View itemView) {
            super(itemView);

            clickLayout = itemView.findViewById(R.id.click_layout);
            iconLayout = itemView.findViewById(R.id.icon_layout);
            icon = itemView.findViewById(R.id.icon);
            title = itemView.findViewById(R.id.title);
            desc = itemView.findViewById(R.id.desc);
            time = itemView.findViewById(R.id.time);
            rest = itemView.findViewById(R.id.day_plan_rest);
            contentLayout = itemView.findViewById(R.id.content_layout);
        }
    }
}
